/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.GrantRequest.agency;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import researchgrants.parts.db.Db;

/**
 * A Category for Agencies
 * Each Agency belongs to a specific AgencyCategory
 * @author DOStudent1
 */
public class AgencyCategory {
    // returns an existing AgencyCategory
    static AgencyCategory getByID(int id) {
        return(new AgencyCategory(id));
    }

    private int id;
    private String agencyCategoryName;
    
    // private constructor
    private AgencyCategory(int id) {
        this.id=id;
        syncDataFromDb();
    }

    // populate the fields from the database
    private void syncDataFromDb() {
        try {
            Connection conn = Db.openDbConnection();
            PreparedStatement prepareStatement = Db.createPreparedStatement(conn, "SELECT AgencyCategoryText FROM tblAgencyCategories WHERE AgencyCategoryID=?");
            prepareStatement.setInt(1, id);
            ResultSet agencyResultSet = prepareStatement.executeQuery();
            Db.moveToFirstLine(agencyResultSet);

            agencyCategoryName=((String) Db.getColumn(agencyResultSet, 1));
            
            agencyResultSet.close();
            prepareStatement.close();
            Db.closeDbConnection(conn);
        } catch (SQLException ex) {
            Logger.getLogger(Agency.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Agency.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    //////////////////////////////////////
    // Retreives the elements' values
    //////////////////////////////////////
    
    public String getAgencyCategoryNameString() {
        return agencyCategoryName;
    }
    
    public String getDescription() {
        return (agencyCategoryName + "(" + Integer.toString(id) + ")");
    }
    
    

}
